﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:ProductInfo
	/// </summary>
	public partial class ProductInfo
	{
		public ProductInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ProductId", "ProductInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ProductId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ProductInfo");
			strSql.Append(" where ProductId=@ProductId");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)
			};
			parameters[0].Value = ProductId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.ProductInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ProductInfo(");
			strSql.Append("ProductImage,ProductType,SeriesId,ProductName,ProductGrade,Color,Material,Voltage,Power,CutOut,Size,ProductStock,Price)");
			strSql.Append(" values (");
			strSql.Append("@ProductImage,@ProductType,@SeriesId,@ProductName,@ProductGrade,@Color,@Material,@Voltage,@Power,@CutOut,@Size,@ProductStock,@Price)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductImage", SqlDbType.NVarChar,80),
					new SqlParameter("@ProductType", SqlDbType.NVarChar,10),
					new SqlParameter("@SeriesId", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,10),
					new SqlParameter("@ProductGrade", SqlDbType.NChar,10),
					new SqlParameter("@Color", SqlDbType.NChar,10),
					new SqlParameter("@Material", SqlDbType.NChar,10),
					new SqlParameter("@Voltage", SqlDbType.Float,8),
					new SqlParameter("@Power", SqlDbType.Int,4),
					new SqlParameter("@CutOut", SqlDbType.Float,8),
					new SqlParameter("@Size", SqlDbType.Float,8),
					new SqlParameter("@ProductStock", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Money,8)};
			parameters[0].Value = model.ProductImage;
			parameters[1].Value = model.ProductType;
			parameters[2].Value = model.SeriesId;
			parameters[3].Value = model.ProductName;
			parameters[4].Value = model.ProductGrade;
			parameters[5].Value = model.Color;
			parameters[6].Value = model.Material;
			parameters[7].Value = model.Voltage;
			parameters[8].Value = model.Power;
			parameters[9].Value = model.CutOut;
			parameters[10].Value = model.Size;
			parameters[11].Value = model.ProductStock;
			parameters[12].Value = model.Price;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.ProductInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ProductInfo set ");
			strSql.Append("ProductImage=@ProductImage,");
			strSql.Append("ProductType=@ProductType,");
			strSql.Append("SeriesId=@SeriesId,");
			strSql.Append("ProductName=@ProductName,");
			strSql.Append("ProductGrade=@ProductGrade,");
			strSql.Append("Color=@Color,");
			strSql.Append("Material=@Material,");
			strSql.Append("Voltage=@Voltage,");
			strSql.Append("Power=@Power,");
			strSql.Append("CutOut=@CutOut,");
			strSql.Append("Size=@Size,");
			strSql.Append("ProductStock=@ProductStock,");
			strSql.Append("Price=@Price");
			strSql.Append(" where ProductId=@ProductId");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductImage", SqlDbType.NVarChar,80),
					new SqlParameter("@ProductType", SqlDbType.NVarChar,10),
					new SqlParameter("@SeriesId", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,10),
					new SqlParameter("@ProductGrade", SqlDbType.NChar,10),
					new SqlParameter("@Color", SqlDbType.NChar,10),
					new SqlParameter("@Material", SqlDbType.NChar,10),
					new SqlParameter("@Voltage", SqlDbType.Float,8),
					new SqlParameter("@Power", SqlDbType.Int,4),
					new SqlParameter("@CutOut", SqlDbType.Float,8),
					new SqlParameter("@Size", SqlDbType.Float,8),
					new SqlParameter("@ProductStock", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@ProductId", SqlDbType.Int,4)};
			parameters[0].Value = model.ProductImage;
			parameters[1].Value = model.ProductType;
			parameters[2].Value = model.SeriesId;
			parameters[3].Value = model.ProductName;
			parameters[4].Value = model.ProductGrade;
			parameters[5].Value = model.Color;
			parameters[6].Value = model.Material;
			parameters[7].Value = model.Voltage;
			parameters[8].Value = model.Power;
			parameters[9].Value = model.CutOut;
			parameters[10].Value = model.Size;
			parameters[11].Value = model.ProductStock;
			parameters[12].Value = model.Price;
			parameters[13].Value = model.ProductId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ProductId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ProductInfo ");
			strSql.Append(" where ProductId=@ProductId");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)
			};
			parameters[0].Value = ProductId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ProductIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ProductInfo ");
			strSql.Append(" where ProductId in ("+ProductIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ProductInfo GetModel(int ProductId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ProductId,ProductImage,ProductType,SeriesId,ProductName,ProductGrade,Color,Material,Voltage,Power,CutOut,Size,ProductStock,Price from ProductInfo ");
			strSql.Append(" where ProductId=@ProductId");
			SqlParameter[] parameters = {
					new SqlParameter("@ProductId", SqlDbType.Int,4)
			};
			parameters[0].Value = ProductId;

			Maticsoft.Model.ProductInfo model=new Maticsoft.Model.ProductInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.ProductInfo DataRowToModel(DataRow row)
		{
			Maticsoft.Model.ProductInfo model=new Maticsoft.Model.ProductInfo();
			if (row != null)
			{
				if(row["ProductId"]!=null && row["ProductId"].ToString()!="")
				{
					model.ProductId=int.Parse(row["ProductId"].ToString());
				}
				if(row["ProductImage"]!=null)
				{
					model.ProductImage=row["ProductImage"].ToString();
				}
				if(row["ProductType"]!=null)
				{
					model.ProductType=row["ProductType"].ToString();
				}
				if(row["SeriesId"]!=null && row["SeriesId"].ToString()!="")
				{
					model.SeriesId=int.Parse(row["SeriesId"].ToString());
				}
				if(row["ProductName"]!=null)
				{
					model.ProductName=row["ProductName"].ToString();
				}
				if(row["ProductGrade"]!=null)
				{
					model.ProductGrade=row["ProductGrade"].ToString();
				}
				if(row["Color"]!=null)
				{
					model.Color=row["Color"].ToString();
				}
				if(row["Material"]!=null)
				{
					model.Material=row["Material"].ToString();
				}
				if(row["Voltage"]!=null && row["Voltage"].ToString()!="")
				{
					model.Voltage=decimal.Parse(row["Voltage"].ToString());
				}
				if(row["Power"]!=null && row["Power"].ToString()!="")
				{
					model.Power=int.Parse(row["Power"].ToString());
				}
				if(row["CutOut"]!=null && row["CutOut"].ToString()!="")
				{
					model.CutOut=decimal.Parse(row["CutOut"].ToString());
				}
				if(row["Size"]!=null && row["Size"].ToString()!="")
				{
					model.Size=decimal.Parse(row["Size"].ToString());
				}
				if(row["ProductStock"]!=null && row["ProductStock"].ToString()!="")
				{
					model.ProductStock=int.Parse(row["ProductStock"].ToString());
				}
				if(row["Price"]!=null && row["Price"].ToString()!="")
				{
					model.Price=decimal.Parse(row["Price"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ProductId,ProductImage,ProductType,SeriesId,ProductName,ProductGrade,Color,Material,Voltage,Power,CutOut,Size,ProductStock,Price ");
			strSql.Append(" FROM ProductInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ProductId,ProductImage,ProductType,SeriesId,ProductName,ProductGrade,Color,Material,Voltage,Power,CutOut,Size,ProductStock,Price ");
			strSql.Append(" FROM ProductInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM ProductInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ProductId desc");
			}
			strSql.Append(")AS Row, T.*  from ProductInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "ProductInfo";
			parameters[1].Value = "ProductId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

